/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jeesite.modules.ajz.web;

import com.github.sd4324530.fastweixin.api.enums.OauthScope;
import com.github.sd4324530.fastweixin.company.api.QYOauthAPI;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.common.web.http.ServletUtils;
import com.jeesite.modules.sys.wx.fw.utils.FwConfigUtil;
import com.jeesite.modules.sys.wx.qy.util.QyConfigUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;

/**
 * 门店区域划分表Controller
 *
 * @author whh
 * @version 2017-09-14
 */
@Controller
@RequestMapping(value = "${adminPath}/ajz/ajzBaseinfo")
public class AjzBaseinfoController extends BaseController {

    @RequestMapping({"ajzType"})
    public String ajzType( HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        String openId = FwConfigUtil.getOpenId(request,response);
        if (StringUtils.isEmpty(openId)) {
            String oauthPageUrl = FwConfigUtil.getOauthAPI().getOauthPageUrl(request.getRequestURL().toString(), OauthScope.SNSAPI_BASE,"1");
            ServletUtils.redirectUrl(request, response, oauthPageUrl);
        }



        System.out.println("openId: " + openId);
        model.addAttribute("openId",openId);
        model.addAttribute("userId","");

        return "modules/ajz/fw/staff";
    }

    @RequestMapping({"qy"})
    public String qy( HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        String userId = QyConfigUtil.getUserId(request,response);
        if (StringUtils.isEmpty(userId)) {
//            String oauthPageUrl =  QyConfigUtil.getOauthAPI().getOauthPageUrl(request.getRequestURL().toString(), OauthScope.SNSAPI_BASE,"1");
            QYOauthAPI qyOauthAPI = QyConfigUtil.getQYOauthAPI();
            String oauthPageUrl = qyOauthAPI.getOauthPageUrl(request.getRequestURL().toString(),OauthScope.SNSAPI_BASE,"1");
            oauthPageUrl = URLDecoder.decode(oauthPageUrl, "utf-8");
            ServletUtils.redirectUrl(request, response, oauthPageUrl);
        }
        System.out.println("userId" + userId);
        model.addAttribute("userId",userId);
        model.addAttribute("openId","");
//        model.addAttribute("userId",userId);
        return "modules/ajz/fw/staff";
    }


    @RequestMapping({"synchSingle"})
    public String synchSingle(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
//        String message = QyConfigUtil.createUser(request);
        model.addAttribute("userId","");
//        model.addAttribute("openId",message);

        return "modules/ajz/fw/staff";
    }

}